<template>
  <div>
    <!-- 面包鞋导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 主体区域 -->
    <el-card>
      <!-- 搜索框 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入关键字"
            v-model="queryInfo.query"
            clearable
            @clear="getAllGoodsList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getAllGoodsList"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="goAddPage">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 数据列表 -->
      <el-table :data="goodsList" border stripe>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name"></el-table-column>
        <el-table-column
          label="商品价格(元)"
          prop="goods_price"
          width="100px"
        ></el-table-column>
        <el-table-column
          label="商品数量"
          prop="goods_number"
          width="100px"
        ></el-table-column>
        <el-table-column
          label="商品重量(kg)"
          prop="goods_weight"
          width="100px"
        ></el-table-column>
        <el-table-column label="添加日期" width="150">
          <template v-slot="scope">
            <span>{{ scope.row.add_time | dateFormat }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200">
          <template v-slot="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="getGoodInfoById(scope.row.goods_id)"
              >编辑</el-button
            >
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="removeGoodById(scope.row.goods_id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页组件 -->
      <el-pagination
        @size-change="pageSizeChange"
        @current-change="pageNumChange"
        :current-page="this.queryInfo.pagenum"
        :page-sizes="[10, 15, 20, 30]"
        :page-size="this.queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="this.total"
        background
      >
      </el-pagination>
    </el-card>
    <!-- 编辑商品对话框 -->
    <el-dialog
      :close-on-click-modal="false"
      :show-close="false"
      title="编辑商品"
      :visible.sync="editDialogVisible"
      width="75%"
      :closed="editDialogClose"
    >
      <el-form ref="editForm" :model="editForm" :rules="FormRules">
        <el-form-item label="商品名称" prop="goods_name">
          <el-input v-model="editForm.goods_name"></el-input>
        </el-form-item>
        <el-form-item label="商品价格" prop="goods_price">
          <el-input v-model="editForm.goods_price" type="number"></el-input>
        </el-form-item>
        <el-form-item label="商品数量" prop="goods_number">
          <el-input v-model="editForm.goods_number" type="number"></el-input>
        </el-form-item>
        <el-form-item label="商品重量" prop="goods_weight">
          <el-input v-model="editForm.goods_weight" type="number"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editGood">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 10,
      },
      goodsList: [],
      total: 0,
      editForm: {},
      editDialogVisible: false,
      FormRules: {
        goods_name: [
          { required: true, message: "请输入商品名称", trigger: "blur" },
        ],
        goods_price: [
          { required: true, message: "请输入商品价格", trigger: "blur" },
        ],
        goods_number: [
          { required: true, message: "请输入商品数量", trigger: "blur" },
        ],
        goods_weight: [
          { required: true, message: "请输入商品重量", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    async getGoodsList() {
      const { data: res } = await this.$http.get("goods", {
        params: this.queryInfo,
      });
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.goodsList = res.data.goods;
      this.total = res.data.total;

      this.$message.success(res.meta.msg);
    },
    getAllGoodsList() {
      this.queryInfo.pagenum = 1;
      this.getGoodsList();
    },
    pageSizeChange(newSize) {
      this.queryInfo.pagesize = newSize;
      this.getGoodsList();
    },
    pageNumChange(newPage) {
      this.queryInfo.pagenum = newPage;
      this.getGoodsList();
    },
    goAddPage() {
      this.$router.push("/goods/add");
    },
    removeGoodById(id) {
      this.$confirm("此操作将永久删除该商品, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const { data: res } = await this.$http.delete("goods/" + id);
          if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
          this.$message.success(res.meta.msg);
          this.getAllGoodsList();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    async getGoodInfoById(id) {
      const { data: res } = await this.$http.get("goods/" + id);
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);

      this.editForm = res.data;
      this.editDialogVisible = true;
      // console.log(this.editForm);
    },
    editDialogClose() {
      this.editForm = {};
    },
    editGood() {
      this.$refs.editForm.validate(async (valid) => {
        if (!valid) return;
        const { data: res } = await this.$http.put(
          "goods/" + this.editForm.goods_id,
          this.editForm
        );
        if (res.meta.status !== 200) return this.$message.error(res.meta.msg);

        this.$message.success(res.meta.msg);
        this.getAllGoodsList();
        this.editDialogVisible = false;
      });
      // console.log(this.editForm);
    },
  },
  created() {
    this.getGoodsList();
  },
};
</script>
<style lang='less' scoped>
</style>